package danny.hoi.controller;

import danny.hoi.pojo.GenVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletResponse;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于下载各类模板
 */
@RestController
@RequestMapping("/gen")
public class GenController {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @GetMapping("/mapper")
    public void genCodeMapper(GenVo genVo, HttpServletResponse response) {
        try {
            genTemplate(genVo, response, "mapper");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/service")
    public void genCodeService(GenVo genVo, HttpServletResponse response) {
        try {
            genTemplate(genVo, response, "service");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/impl")
    public void genCodeImpl(GenVo genVo, HttpServletResponse response) {
        try {
            genTemplate(genVo, response, "serviceImpl");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/controller")
    public void genCode(GenVo genVo, HttpServletResponse response) {
        try {
            genTemplate(genVo, response, "controller");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void genTemplate(GenVo genVo, HttpServletResponse response, String type) throws Exception {

        Configuration configuration = freeMarkerConfigurer.getConfiguration();

        //4.加载数据
        Map<String, Object> dataModel = new HashMap<>();

        //pojo全路径名
        String pojoFullName = genVo.getPojo();
        //mapper全路径名
        String mapper = pojoFullName.substring(0, pojoFullName.lastIndexOf("."))
                .replace("pojo", "") + "mapper";
        //pojo名,首字母大写
        String pojo = firstCaseToUpper(pojoFullName.substring(pojoFullName.lastIndexOf(".") + 1));
        //业务类
        String servicePackage = genVo.getService().substring(0, genVo.getService().lastIndexOf("."));
        String service = genVo.getService().substring(genVo.getService().lastIndexOf(".") + 1);
        //控制器类
        String controllerPackage = genVo.getController().substring(0, genVo.getController().lastIndexOf("."));
        String controller = genVo.getController().substring(genVo.getController().lastIndexOf(".") + 1);

        dataModel.put("pojoFullName", pojoFullName);
        dataModel.put("mapperPackage", mapper);
        dataModel.put("pojo", pojo);
        dataModel.put("servicePackage", servicePackage);
        dataModel.put("service", service);
        dataModel.put("controllerPackage", controllerPackage);
        dataModel.put("controller", controller);
        String tempPojo = pojo.replace("Tb", "");
        //获取模板
        Template template = configuration.getTemplate(type + ".ftl");
        response.setContentType("application/otect-stream");
        //设置响应信息
        response.addHeader("Content-Disposition", "attachment;filename=" + tempPojo + firstCaseToUpper(type) + ".java");
        Writer writer = response.getWriter();

        //5.获取模板
        template.process(dataModel, writer);
        writer.close();
    }


    private String firstCaseToUpper(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

}
